package de.mmis.core.base.abstracttree.serializer.internal;

import java.util.Map;

import de.mmis.core.base.DeserializableAnnotations.DeserializationMethod;
import de.mmis.core.base.abstracttree.InnerNode;
import de.mmis.core.base.abstracttree.LeafNode;
import de.mmis.core.base.abstracttree.Tree;
import de.mmis.core.base.abstracttree.serializer.Serializable;
import de.mmis.core.base.abstracttree.serializer.Serializer;
import de.mmis.core.base.abstracttree.serializer.SerializerHelper;
import de.mmis.core.base.abstracttree.serializer.selector.SerializableSelector;

/**
 * Serializer class for (de)serializing objects implementing the
 * {@link Serializable}-interface from/to Trees
 * 
 * @author Christian Koch
 */
public class SerializableSerializer implements Serializer<Serializable> {

	@DeserializationMethod
	public static Object deserialize(Tree[] exps, ClassLoader classLoader,
			String id, Map<String, Object> idmap) {

		return new SerializableSelector(exps, classLoader, id, idmap);
	}

	@Override
	public Tree serialize(Serializable object, boolean explicit,
			Map<Object, String> refmap, ClassLoader classLoader) {

		String type = object.getTypeIdentifier();

		Tree[] exps = object.serialize(explicit, refmap, classLoader);

		InnerNode ret = new InnerNode();

		if (explicit)
			ret.add(new LeafNode(SerializerHelper.typeIdentifierPrefix
					+ (type == null ? "serializable" : type)));

		for (Tree exp : exps)
			ret.add(exp);

		return ret;
	}
}
